package com.dp.server;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.dp.server.CargarArchivo;
import com.dp.server.entities.Almacen;

public class ISubirArchivo3 extends HttpServlet //Cargar plan de vuelo
{
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doGet(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		FileItemFactory factory = new DiskFileItemFactory();

		// Create a new file upload handler
		ServletFileUpload upload = new ServletFileUpload(factory);
		try
		{
			List<FileItem> items = upload.parseRequest(req);
			IServiciosGeograficosImpl geo = new IServiciosGeograficosImpl();
			CargarArchivo archivo = new CargarArchivo();
			for (FileItem fileItem : items)
			{
				InputStream in_stream = fileItem.getInputStream();
				java.util.Scanner in_scanner = new java.util.Scanner( in_stream );
				resp.getWriter().println(archivo.RegistrarVuelo(in_scanner, resp));
			}
		}
		catch (Exception e)
		{
			resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,"Ocurrio un error al leer el archivo : " + e.getMessage());
		}
	}
}


